package edu.hawaii.myisern.action;

import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebResponse;

/**
 * Demonstrates that the login, logout, and security filter are all working.
 * 
 * @author Edward Kim
 * @author JianFei Liao
 * @author KaYee Leung
 */
public class TestAuthentication {
  /** Get the test host. */
  private String testHost = System.getProperty("test.host");
  /** A string represent the homepage title */
  private static final String HOMEPAGE_TITLE = "MyISERN 1.3 - Home";
  /** A string represent the password attribute */
  private static final String PASSWORD_FIELD = "password";
  
  /**
   * Tests the authetication.
   * 
   * @throws Exception If error occur.
   */
  @Test
  public void testViewAllResearchers() throws Exception {
    WebConversation conversation = new WebConversation();
    
    // retrieves the index.jsp page and check the title
    String url = this.testHost + "myisern-gold";
    WebResponse response = conversation.getResponse(url);
    assertEquals("Checking index.jsp retrieval", HOMEPAGE_TITLE, response.getTitle());
    
    // tries to click the "View All Researchers" link without login
    WebLink link = response.getLinkWith("View All Researchers");
    response = link.click();
    // the page should still be index.jsp
    assertEquals("Clicking researchers link without login", 
      HOMEPAGE_TITLE, response.getTitle());
    
    // tries to login using wrong user name
    WebForm loginForm = response.getFormWithID("loginForm");
    loginForm.setParameter("username", "wrong user name");
    loginForm.setParameter(PASSWORD_FIELD, "password");
    response = loginForm.submit();
    // the page should still be index.jsp
    assertEquals("Clicking link without login", 
      HOMEPAGE_TITLE, response.getTitle());
    
    // tries to login using wrong password
    loginForm.setParameter("username", "isern");
    loginForm.setParameter(PASSWORD_FIELD, "wrong password");
    response = loginForm.submit();
    // the page should still be index.jsp
    assertEquals("Clicking link without login", 
      HOMEPAGE_TITLE, response.getTitle());
    
    // login the system
    loginForm.setParameter("username", "isern");
    loginForm.setParameter(PASSWORD_FIELD, "password");
    response = loginForm.submit();
    
    // check if the user is login successfully by clicking the "View All Researchers" link again
    link = response.getLinkWith("View All Researchers");
    response = link.click();
    // the page should become researchers.jsp
    assertEquals("Clicking link after login", 
        "MyISERN 1.3 - View All Researchers", response.getTitle());
    
    // tries to logout the system
    link = response.getLinkWith("Logout");
    response = link.click();
    // the page should go back to index.jsp
    assertEquals("Clicking link after login", 
        HOMEPAGE_TITLE, response.getTitle());
    
    // tries to click the other link after logout
    link = response.getLinkWith("View All Organizations");
    response = link.click();
    // the page should still be index.jsp
    assertEquals("Clicking organizations link without login", 
      HOMEPAGE_TITLE, response.getTitle());
  } 
}